<?php

namespace App\Containers\AppSection\Qualification\Actions;

use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Containers\AppSection\Qualification\Tasks\FindQualificationByIdTask;
use App\Containers\AppSection\Qualification\Tasks\UpdateQualificationTask;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class ModeratorCheckQualificationAction extends Action
{

    public function run($id, Request $request)
    {
        $qualification = app(FindQualificationByIdTask::class)->run($id);

        $data = [
            'moderator_id' => $request->profile()->id,
            'status_key' => $request->getInputByKey('type'),
        ];

        if ($request->getInputByKey('type') === 'qualification_qualified') {
            $data['qualified_at'] = now();
        }

        if ($request->getInputByKey('type') === 'qualification_rejected') {
            $data['unqualified_at'] = now();
            $data['remark'] = $request->getInputByKey('remark');
        }

        app(UpdateProfileTask::class)->run($qualification->profile->id, [
            'qualification_status_key' => $request->getInputByKey('type'),
        ]);


        app(UpdateQualificationTask::class)->run($qualification->id, $data);


        $log = '';
        switch ($request->getInputByKey('type')) {
            case 'qualification_qualified':
                $log = "Профиль №{$qualification->profile->id} квалифицирован";
                break;
            case 'qualification_rejected':
                $log = "Профиль №{$qualification->profile->id}. Квалификация отклонена";
                break;
        }

        app(UpdateQualificationTask::class)->run($qualification->id, ['updated_at' => now()]);

        app(CreateLogTask::class)->run($qualification->profile->id, $log . ' (модератор)', 'qualification', $qualification->id);
        app(CreateLogTask::class)->run($request->profile()->id, $log, 'qualification', $qualification->id);
    }
}
